package com.scpii.universal.struts.manage;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.EnterShopDto;
import com.scpii.universal.dto.ShopPOJO;
import com.scpii.universal.hibernate.entity.EnterpShopTab;
import com.scpii.universal.pns.entity.Tags;
import com.scpii.universal.service.EnterShopManageService;
import com.scpii.universal.service.EnterpShopService;
import com.scpii.universal.util.FileUploadUtil;
import com.scpii.universal.util.ImgAndFilePath;
import com.scpii.universal.util.StreamUtil;
import com.scpii.universal.util.UtilTool;

@Controller
@Scope("prototype")
public class EnterShopManageAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 5403035634603088760L;
	@Resource
	private EnterShopManageService enterShopManageService;
	@Resource
	private EnterpShopService enterpShopService;
	// 企业信息的主键
	private Integer intEnterpId;
	// 企业门店信息
	private List<EnterShopDto> shopList;
	// 查询条件
	private EnterShopDto condition;

	// 企业门店的主键
	private Integer intShopId;

	// 企业门店封装
	private EnterpShopTab enterpShopTab;

	// 分页信息
	private Pagination pageContent = new Pagination();
	
	private File iconFile;
	private String iconFileFileName;
	private File thumbFile;
	private String thumbFileFileName;
	private String newTagsContainer;
	private String selectedTagsContainer;
	private String delTagsContainer;
	private List<Tags> usedTags;
	private String intCateId;

	public Integer getIntShopId() {
		return intShopId;
	}

	public void setIntShopId(Integer intShopId) {
		this.intShopId = intShopId;
	}

	public EnterpShopTab getEnterpShopTab() {
		return enterpShopTab;
	}

	public void setEnterpShopTab(EnterpShopTab enterpShopTab) {
		this.enterpShopTab = enterpShopTab;
	}

	public Pagination getPageContent() {
		return pageContent;
	}

	public void setPageContent(Pagination pageContent) {
		this.pageContent = pageContent;
	}

	public EnterShopDto getCondition() {
		return condition;
	}

	public void setCondition(EnterShopDto condition) {
		this.condition = condition;
	}

	public List<EnterShopDto> getShopList() {
		return shopList;
	}

	public void setShopList(List<EnterShopDto> shopList) {
		this.shopList = shopList;
	}

	public Integer getIntEnterpId() {
		return intEnterpId;
	}

	public void setIntEnterpId(Integer intEnterpId) {
		this.intEnterpId = intEnterpId;
	}

	public File getIconFile() {
		return iconFile;
	}

	public void setIconFile(File iconFile) {
		this.iconFile = iconFile;
	}

	public String getIconFileFileName() {
		return iconFileFileName;
	}

	public void setIconFileFileName(String iconFileFileName) {
		this.iconFileFileName = iconFileFileName;
	}

	public File getThumbFile() {
		return thumbFile;
	}

	public void setThumbFile(File thumbFile) {
		this.thumbFile = thumbFile;
	}

	public String getThumbFileFileName() {
		return thumbFileFileName;
	}

	public void setThumbFileFileName(String thumbFileFileName) {
		this.thumbFileFileName = thumbFileFileName;
	}

	/**
	 * 获取企业门店的详细信息
	 * 
	 * @return
	 * @author zhangqiaing 2012-7-17下午02:48:00
	 */
	public String getEnterShopinfo() {
		int intContentId = UtilTool.StringToInteger(request.getParameter("contentId"));
		if (condition == null) {
			condition = new EnterShopDto();
		}
		condition.setIntContentId(intContentId);
		//设置门店列表关联的内容ID到Session中
		session.setAttribute("enterpShopContentId",intContentId);
		
		// 设置appid
		condition.setIntAppId(getUserInfo().getAppId());
		condition.setIntEnterpId(getUserInfo().getIntEnterpId());

		// 判断第一次进入分页信息
		if (getSelectPerPage("pageContent") == -1) {
			pageContent.setPageRecord(GenericConstants.PAGE_NO_10);
		} else {
			pageContent.setPageRecord(getSelectPerPage("pageContent"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("pageContent") == -1) {
			pageContent.setTotalRecord(enterShopManageService
					.getCountByCondition(condition,null));
		} else {
			pageContent.setTotalRecord(getQueryFlag("pageContent"));
		}
		setPageParameters(pageContent, "pageContent");
		shopList = enterShopManageService.getEnterShopList(condition,null,
				pageContent.getCurrentPage(), pageContent.getPageRecord());

		return SUCCESS;
	}
	
	/**
	 * 根据内容ID，获取和内容相关的地理位置列表
	 * @return
	 * @author 甘建新
	 * 2012-9-21上午02:06:29
	 */
	public String getEnterpShopListByContentId(){
		int intContentId = UtilTool.StringToInteger(request.getParameter("contentId"));
		if (intContentId>0) {
//			enterShopManageService.getEnterpShopListByContentId(condition, pageNo, pageSize);
		}
		return null;
	}

	/**
	 * 保存编辑信息
	 * @return
	 * @author zhangqiaing
	 * 2012-7-18下午03:09:58
	 * @throws IOException 
	 */
    public String doEditEnterShop() throws IOException{
    	EnterpShopTab enterTab =enterpShopService.get(enterpShopTab.getIntShopId());
    	//门店名称
    	enterTab.setVarShopName(enterpShopTab.getVarShopName());
    	enterTab.setVarShopCountry(enterpShopTab.getVarShopCountry());
    	enterTab.setVarShopCity(enterpShopTab.getVarShopCity());
    	enterTab.setVarShopProvince(enterpShopTab.getVarShopProvince());
    	enterTab.setVarShopLongitude(enterpShopTab.getVarShopLongitude());
    	enterTab.setVarShopLatitude(enterpShopTab.getVarShopLatitude());
    	enterTab.setVarShopPhonenum(enterpShopTab.getVarShopPhonenum());
    	//添加的内容ID
    	enterTab.setIntContentId(enterpShopTab.getIntContentId());
    	
    	enterTab.setVarShopPostnum(enterpShopTab.getVarShopPostnum());
    	enterTab.setVarShopAddress(enterpShopTab.getVarShopAddress());
    	enterTab.setVarDesc(enterpShopTab.getVarDesc());
    	enterTab.setVarBusinessArea(enterpShopTab.getVarBusinessArea());
    	enterTab.setVarOpenTime(enterpShopTab.getVarOpenTime());
    	String icon = enterTab.getVarIconUrl();
    	String thumb = enterTab.getVarThumbImg();
    	String newIcon = imgProcess(iconFile, iconFileFileName, enterTab.getVarIconUrl());
    	String newThumb = imgProcess(thumbFile, thumbFileFileName, enterTab.getVarThumbImg());
    	//icon
    	enterTab.setVarIconUrl(newIcon);
    	//thumb
    	enterTab.setVarThumbImg(newThumb);
    	enterpShopService.updateShop(enterTab,getShopParams());
    	oldImgDelete(icon,newIcon);
    	oldImgDelete(thumb,newThumb);
    	jsonOut("成功");
    	return null;
    }

	private String imgProcess(File file,String name,String oldImgUrl) {
		if (file != null) {
			String imgRelativePath = getImgBasePath() + ImgAndFilePath.SHOP + File.separator;
			String imgAbsolutePath = getWebAppsRealPath() + imgRelativePath;
			File folder = new File(imgAbsolutePath);
			if (!folder.exists()) {
				folder.mkdirs();
			}
			String fileName = name.substring(0,name.lastIndexOf("."))+ "_"+ FileUploadUtil.generateIconName()
					+ "." + FilenameUtils.getExtension(name);
			StreamUtil.uploadActiveImgs(file, imgAbsolutePath + fileName);
			return imgRelativePath.replaceAll("\\\\", "/")+fileName;
		}
		return oldImgUrl;
	}
	
	private void oldImgDelete(String oldImgUrl,String newImgUrl) {
		if (!StringUtils.isBlank(oldImgUrl) && !newImgUrl.equals(oldImgUrl)){
			File newFile = new File(getWebAppsRealPath() + oldImgUrl);
			if (newFile.exists()) {
				newFile.delete();
			}
		}
	}
	
    /**
     * 删除企业门店信息
     * @return
     * @author zhangqiaing
     * 2012-7-18下午04:02:30
     */
    public String dodelEnterShop(){
    	boolean isBinded = Boolean.parseBoolean(request.getParameter("isBinded"));
    	String fromUrl = request.getParameter("fromUrl");
    	EnterpShopTab enterTab  = enterpShopService.get(intShopId);    	
    	enterTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
    	enterpShopService.update(enterTab,fromUrl,isBinded);
    	
    	return SUCCESS;
    }
    /**
     * 增加企业门店信息
     * @return
     * @author zhangqiaing
     * 2012-7-18下午04:59:01
     * @throws IOException 
     */
    public String doaddEnterShop() throws IOException{
    	
    	EnterpShopTab enterTab =new EnterpShopTab();
    	enterTab.setIntAppId(getUserInfo().getAppId());
    	enterTab.setIntEnterpId(getUserInfo().getIntEnterpId());
    	//门店名称
    	enterTab.setVarShopName(enterpShopTab.getVarShopName());
    	enterTab.setVarShopCountry(enterpShopTab.getVarShopCountry());
    	enterTab.setVarShopCity(enterpShopTab.getVarShopCity());
    	enterTab.setVarShopProvince(enterpShopTab.getVarShopProvince());
    	enterTab.setVarShopLongitude(enterpShopTab.getVarShopLongitude());
    	enterTab.setVarShopLatitude(enterpShopTab.getVarShopLatitude());
    	enterTab.setVarShopPhonenum(enterpShopTab.getVarShopPhonenum());
    	//添加的地理位置内容ID
    	enterTab.setIntContentId(enterpShopTab.getIntContentId());
    	
    	enterTab.setVarShopPostnum(enterpShopTab.getVarShopPostnum());
    	enterTab.setVarShopAddress(enterpShopTab.getVarShopAddress());
    	enterTab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
    	enterTab.setIntCateId(0);
    	//icon
    	enterTab.setVarIconUrl(imgProcess(iconFile, iconFileFileName, null));
    	//thumb
    	enterTab.setVarThumbImg(imgProcess(thumbFile, thumbFileFileName, null));
    	enterTab.setVarDesc(enterpShopTab.getVarDesc());
    	enterTab.setVarBusinessArea(enterpShopTab.getVarBusinessArea());
    	enterTab.setVarOpenTime(enterpShopTab.getVarOpenTime());
    	enterpShopService.updateShop(enterTab, getShopParams());
    	jsonOut("成功");
    	return null;
    }
    
    public String doDelTag() throws IOException {
    	String tagId = request.getParameter("tagId");
    	enterpShopService.deleteTag(Long.parseLong(tagId));
    	jsonOut("成功");
    	return null;
    }
    
    public String doEditTag() throws IOException {
    	String tagId = request.getParameter("tagId");
    	String value = request.getParameter("value");
    	enterpShopService.updateTag(Long.parseLong(tagId),value);
    	jsonOut("成功");
    	return null;
    }
    
    public String doAddTag() throws IOException {
    	String cateId = request.getParameter("cateId");
    	String value = request.getParameter("value");
    	value = URLDecoder.decode(URLDecoder.decode(value,"UTF-8"), "UTF-8");
    	enterpShopService.saveTag(value,GenericConstants.CATE_ID_PRE + Long.parseLong(cateId));
    	jsonOut("成功");
    	return null;
    }
    
    public String doUpdateShopTags() {
    	String tags = request.getParameter("tags");
    	String shops = request.getParameter("shops");
    	enterpShopService.updateShopTags(tags, shops);
    	return null;
    }
    
    private ShopPOJO getShopParams(){
		ShopPOJO params = new ShopPOJO();
		params.setDelTagsContainer(delTagsContainer);
		params.setNewTagsContainer(newTagsContainer);
		params.setSelectedTagsContainer(selectedTagsContainer);
		return params;
	}
    
    public String updateShopLocation() {
    	return null;
    }

	public String getNewTagsContainer() {
		return newTagsContainer;
	}

	public void setNewTagsContainer(String newTagsContainer) {
		this.newTagsContainer = newTagsContainer;
	}

	public String getSelectedTagsContainer() {
		return selectedTagsContainer;
	}

	public void setSelectedTagsContainer(String selectedTagsContainer) {
		this.selectedTagsContainer = selectedTagsContainer;
	}

	public String getDelTagsContainer() {
		return delTagsContainer;
	}

	public void setDelTagsContainer(String delTagsContainer) {
		this.delTagsContainer = delTagsContainer;
	}

	public List<Tags> getUsedTags() {
		return usedTags;
	}

	public void setUsedTags(List<Tags> usedTags) {
		this.usedTags = usedTags;
	}

	public String getIntCateId() {
		return intCateId;
	}

	public void setIntCateId(String intCateId) {
		this.intCateId = intCateId;
	}

}
